import { Shape } from "@/mdesign-lib/mdesign-graph.umd.js";
import { TreeDropDto } from "@/mdesign-lib/mdesign-app.umd.js";
import { Action } from "@/mdesign-lib/mdesign-app.umd.js";
import { PropertyKey } from "@/mdesign-lib/mdesign-app.umd.js";
import { ActionKey } from "@/mdesign-lib/mdesign-app.umd.js";
import { actionManager } from "@/mdesign-lib/mdesign-app.umd.js";
import { QuickCreateDiagramAction } from "@/mdesign-lib/mdesign-app.umd.js";
import { MenuItem } from "@/mdesign-lib/mdesign-app.umd.js";
import { modelService, shapeService } from "@/mdesign-lib/mdesign-app.umd.js";

export class CreateBehaviorTreeDiagramAction extends Action {
  keyName = "createBehaviorTreeDiagram" as ActionKey;
  desc = "createBehaviorTreeDiagram";

  async run(item: MenuItem, option?: any) {
    const action = (await actionManager.getAction(ActionKey.QuickCreateDiagram)) as QuickCreateDiagramAction;
    const { baseSt, replaceTab, projectId, modelId } = option;
    const diagramModel = await action.createDiagram(projectId, modelId, [], baseSt, replaceTab);
    const contextId = (await modelService.getPropertyValueById(projectId, diagramModel!.id, PropertyKey.Diagram_context)) as string;
    const shapes = (await shapeService.getShapesByModelId(projectId, diagramModel!.id)) as Shape[];
    const diagramShape = shapes[0];
    if (diagramShape) {
      const params: TreeDropDto = {
        projectId: (app.activeProject as any).id,
        point: { x: 100, y: diagramShape.bounds.height / 2 },
        shapeId: diagramShape.id,
        modelId: contextId,
        diagramId: diagramShape.id
      };
      shapeService.treeDropShape(params);
    }
  }
}
